package com.cloudservice.ccs.controller;

import com.cloudservice.ccs.biz.IWorkOrderBiz;
import com.cloudservice.ccs.model.ApiResult;
import com.cloudservice.ccs.model.PageData;
import com.cloudservice.ccs.entity.WorkOrder;
import com.cloudservice.ccs.model.WorkOrderConnectQuery;
import com.cloudservice.ccs.model.WorkOrderWithCustomerName;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/work-order")
public class WorkOrderController {
    @Autowired
    private IWorkOrderBiz workOrderBiz;

    @PostMapping("/")
    public ApiResult save(@RequestBody WorkOrder workOrder) {
        // 接口问题待讨论：是否应当返回生成的 id？
        workOrderBiz.save(workOrder);
        return ApiResult.success();
    }

    @PutMapping("/{id}")
    public ApiResult update(@PathVariable Integer id, @RequestBody WorkOrder workOrder) {
        workOrderBiz.update(id, workOrder);
        WorkOrder result = workOrderBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @PutMapping("/{id}/partial")
    public ApiResult partialUpdate(@PathVariable Integer id, @RequestBody Map<String, Object> fields) {
        workOrderBiz.partialUpdate(id, fields);
        WorkOrder result = workOrderBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @DeleteMapping("/{id}")
    public ApiResult delete(@PathVariable Integer id) {
        workOrderBiz.delete(id);
        return ApiResult.success();
    }

    @DeleteMapping("/{partial}/deletePartial")
    public ApiResult deletePartial(@PathVariable String partial) {
        String[] temp = partial.split(",");
        for(String s:temp){
            Integer id = Integer.parseInt(s);
            workOrderBiz.delete(id);
        }
        return ApiResult.success();
    }

    @GetMapping("/{id}")
    public ApiResult queryById(@PathVariable Integer id) {
        WorkOrder result = workOrderBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @RequiresPermissions("all")
    @GetMapping("/")
    public ApiResult queryAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit) {
        PageHelper.startPage(page, limit);
        List<WorkOrderConnectQuery> result = workOrderBiz.getAll();
        return ApiResult.successWithData(PageData.fromPage((Page<WorkOrderConnectQuery>) result));
    }

    @PostMapping("/condition")
    public ApiResult queryWithConditions(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit, @RequestBody Map<String, Object> conditions) {
        PageHelper.startPage(page, limit);
        List<WorkOrderConnectQuery> result = workOrderBiz.getWithConditions(conditions);
        return ApiResult.successWithData(PageData.fromPage((Page<WorkOrderConnectQuery>) result));
    }

    @GetMapping("/{id}/connect")
    public ApiResult queryCustomer(@PathVariable Integer id){
        WorkOrderConnectQuery workOrderConnectQuery = workOrderBiz.queryCustomer(id);
        return ApiResult.successWithData(workOrderConnectQuery);

    }

    @GetMapping("/customer/{customerId}")
    public ApiResult queryByCustomerId(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit, @PathVariable Integer customerId) {
        PageHelper.startPage(page, limit);
        List<WorkOrderWithCustomerName> result = workOrderBiz.queryByCustomerId(customerId);
        return ApiResult.successWithData(PageData.fromPage((Page<WorkOrderWithCustomerName>) result));
    }
}
